package com.learn;

import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;

import javax.swing.*;

public class ModifyWindow extends JPanel {
	JLabel[] labels;
	String[] labelNames = { "输入要修改信息的学号：", "（新）姓名：", "（新）班级：", "（新）专业：", "（新）社团：", "（新）职位：" };
	JTextField numberField,nameField,clazzField,majorField,commuField,positionField;
	JButton modifytButton, resetButton, queryButton;
	DBConnection dbc;

	public void init() {
		dbc = new DBConnection();
		Font labelFont = new Font("宋体", Font.PLAIN, 20); // 标签字体
		labels = new JLabel[6];
		for (int i = 0; i < 6; i++) {
			labels[i] = new JLabel(labelNames[i], JLabel.CENTER);
			labels[i].setFont(labelFont);
		}
		queryButton = new JButton("查询");
		queryButton.setFont(labelFont);
		numberField=new JTextField(20);
		numberField.setFont(labelFont);
		nameField=new JTextField(20);
		nameField.setFont(labelFont);
		clazzField=new JTextField(20);
		clazzField.setFont(labelFont);
		majorField=new JTextField(20);
		majorField.setFont(labelFont);
		commuField=new JTextField(20);
		commuField.setFont(labelFont);
		positionField=new JTextField(20);
		positionField.setFont(labelFont);


		modifytButton = new JButton("修改");
		modifytButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				update();
			}
		});

		queryButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				try {
					query();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});

		resetButton = new JButton("重置");
		resetButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				numberField.setText("");
				nameField.setText("");
				clazzField.setText("");
				majorField.setText("");
				commuField.setText("");
				positionField.setText("");
			}
		});

		Box[] horiBoxes = new Box[6];
		for (int i = 0; i < 6; i++) {
			horiBoxes[i] = Box.createHorizontalBox();
			horiBoxes[i].add(labels[i]);
		}
		horiBoxes[0].add(numberField);
		horiBoxes[0].add(queryButton);
		horiBoxes[1].add(nameField);
		horiBoxes[2].add(clazzField);
		horiBoxes[3].add(majorField);
		horiBoxes[4].add(commuField);
		horiBoxes[5].add(positionField);

		
		
		Box verticalBox = Box.createVerticalBox();
		for (int i = 0; i < 6; i++) {
			verticalBox.add(horiBoxes[i]);
			verticalBox.add(Box.createVerticalStrut(15));
		}

		JPanel centerPanel = new JPanel();
		centerPanel.setOpaque(false);
		centerPanel.add(verticalBox);
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(modifytButton);
		buttonPanel.add(resetButton);

		// 设置边界式布局
		this.setLayout(new BorderLayout());
		this.add(centerPanel, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);
	}

	public ModifyWindow() {
		init();
	}


	private void update() {
		String number = numberField.getText();
		String name = nameField.getText();
		if (name.isEmpty()) {
			JOptionPane.showMessageDialog(this, "姓名不能为空", "消息对话框", JOptionPane.CANCEL_OPTION);
			return;
		}
		String classname = clazzField.getText();
		
		if (classname.isEmpty()) {
			JOptionPane.showMessageDialog(this, "班级不能为空", "消息对话框", JOptionPane.CANCEL_OPTION);
			return;
		}
		
		String major = majorField.getText();
		String commu = commuField.getText();
		String position = positionField.getText();

		String[] values = { name, classname, major, commu, position, number };
		if (dbc.updateRecord(values)) {
			JOptionPane.showMessageDialog(this, "修改成功", "消息对话框", JOptionPane.PLAIN_MESSAGE);
		} else {
			JOptionPane.showMessageDialog(this, "修改失败", "消息对话框", JOptionPane.WARNING_MESSAGE);
		}
	}

	private void query() throws SQLException {
		String number = numberField.getText();
		if (dbc.queryRecord(number)) {
			JOptionPane.showMessageDialog(this, "该学号存在", "消息对话框", JOptionPane.PLAIN_MESSAGE);
		} else {
			JOptionPane.showMessageDialog(this, "您输入的学号不存在，请重新输入", "消息对话框", JOptionPane.WARNING_MESSAGE);
		}
	}

	
}
